/**
 * Implement intersection between primitives. 
 * @author Songgang Xu (sxu at tamu dot edu)
 * @file intersect.h
 */

#ifndef _XMESH_INTERSECT_3D_H
#define _XMESH_INTERSECT_3D_H

#include "primitive.h"
#include "predicate_3d.h"

////////////////////NEW VERSION OF INTERSECTION///////////////

namespace math 
{

// We assume this segment p1, r1 has intersection with plane defined by p2, q2, r2; 
template <typename value_type, unsigned dim>
inline vector<value_type, dim> intersect_seg_plane (const vector<value_type, dim> & p1, const vector<value_type, dim> & r1, 
															const vector<value_type, dim> & p2, const vector<value_type, dim> & q2, const vector<value_type, dim> & r2)
{
	vector<value_type, dim> v1, v2, N2; 
	value_type alpha; 
	N2  = (q2 - p2) ^ (r2 - p2) ;
	v1  = p1 - p2;
	v2  = p1 - r1;
	alpha = (v1*N2) / (v2*N2);
	v1  = v2 * alpha ;
	return ( p1 - v1 );
}


template <typename value_type>
	bool intersect_polygon_segment (const Polygon <value_type, 3> & _pol, const Segment <value_type, 3> & _segment, vector <value_type, 3> & incp)
{
	bool flag = predicate_convex_polygon_segment(_pol, _segment) ; 
	incp = intersect_seg_plane (_segment.p0, _segment.p1, _pol[0], _pol[1], _pol[2]); 
	return flag; 
}

}

#endif
